package tmhm.shemes;


import java.awt.Point;
import java.util.ArrayList;

import tmhm.components.*;
import tmhm.gui.GuiLabel;
import tmhm.gui.GuiLine;
import tmhm.gui.GuiScheme;


public class Fetch1 {

    private static REG PC;
    private static TSB TSBPCout1, TSBPCHout3, TSBPCLout3,  TSBIRDAout3, TSBIRJAout2, TSBIRPOMout3, TSBIRBRout3;
    private static IntToInt PChigh, PClow, IBUS1lowINT;
    private static IntToBools IBUS1lowBOOLS;
    private static REG IR0, IR1, IR2, IR3;
    private static BoolsToInt IR0ToINT, IR1ToINT, IR2ToINT, IR3ToINT, IR23_8xToINT;
    private static BoolsToInt IRDAint, IRJAint, IRPOMint, IRBRint;
    //public static BoolsToInt JDISP, INTENTRY, PCRDISP, IMM;
    // potrebni samo za iscrtavanje linija za prosirivanje znakom ili nulom
    private static GuiScheme gui;

    public static GuiScheme getGui() {
        return gui;
    }

    public static void init() {

        PC = new REG(1, "PC");
        PC.getOut(0).setIsInt();
        PC.getOut(0).setNumOfLines(16);
        PC.initVal(256);//100h

        TSBPCout1 = new TSB();
        TSBPCout1.getOut(0).setNumOfLines(16);

        PChigh = new IntToInt(16, 8, true, false);
        PClow = new IntToInt(16, 8, false, false);
        
        TSBPCHout3 = new TSB();
        TSBPCHout3.getOut(0).setNumOfLines(8);
        
        TSBPCLout3 = new TSB();
        TSBPCLout3.getOut(0).setNumOfLines(8);
        
        IBUS1lowINT = new IntToInt(16, 8);
        IBUS1lowBOOLS = new IntToBools(8, 8);

        IR0 = new REG(8, "IR0");
        IR1 = new REG(8, "IR1");
        IR2 = new REG(8, "IR2");
        IR3 = new REG(8, "IR3");

        IR0ToINT = new BoolsToInt(8, 8);
        IR1ToINT = new BoolsToInt(8, 8);
        IR2ToINT = new BoolsToInt(8, 8);
        IR3ToINT = new BoolsToInt(8, 8);
        IR23_8xToINT = new BoolsToInt(8, 8);

        IRDAint = new BoolsToInt(16, 16);
        IRJAint = new BoolsToInt(16, 16);
        IRPOMint = new BoolsToInt(16, 16);
        IRBRint = new BoolsToInt(8, 8);

        TSBIRDAout3 = new TSB();
        TSBIRDAout3.getOut(0).setNumOfLines(16);

        TSBIRJAout2 = new TSB();
        TSBIRJAout2.getOut(0).setNumOfLines(16);

        TSBIRPOMout3 = new TSB();
        TSBIRPOMout3.getOut(0).setNumOfLines(16);

        TSBIRBRout3 = new TSB();
        TSBIRBRout3.getOut(0).setNumOfLines(8);


    }

    public static void connect() {
    	
        PC.setInputPin(0, Bus1.IBUS2());
        PC.setPinInc(Oper1.incPC());
        PC.setPinLd(Oper1.ldPC());

        TSBPCout1.setInputPin(0, PC.getOut(0));
        TSBPCout1.setCtrl(Oper1.PCout1());
        Bus1.addOnIBUS1(TSBPCout1.getOut(0));

        PChigh.setInputPin(0, PC.getOut(0));
        PClow.setInputPin(0, PC.getOut(0));
        
        TSBPCHout3.setInputPin(0, PChigh.getOut(0));
        TSBPCHout3.setCtrl(Oper1.PCHout3());
        Bus1.addOnIBUS3(TSBPCHout3.getOut(0));

        TSBPCLout3.setInputPin(0, PClow.getOut(0));
        TSBPCLout3.setCtrl(Oper1.PCLout3());
        Bus1.addOnIBUS3(TSBPCLout3.getOut(0));
        
        IBUS1lowINT.setInputPin(0, Bus1.IBUS1());
        IBUS1lowBOOLS.setInputPin(0, IBUS1lowINT.getOut(0));

        IR0.setPinLd(Oper1.ldIR0());
        IR1.setPinLd(Oper1.ldIR1());
        IR2.setPinLd(Oper1.ldIR2());
        IR3.setPinLd(Oper1.ldIR3());
        for (int i = 0; i < 8; i++) {
            Pin p = IBUS1lowBOOLS.getOut(i);
            IR0.setInputPin(i, p);
            IR1.setInputPin(i, p);
            IR2.setInputPin(i, p);
            IR3.setInputPin(i, p);
        }

        for (int i = 0; i < 8; i++) {
            IR0ToINT.setInputPin(i, IR0.getOut(i));
            IR1ToINT.setInputPin(i, IR1.getOut(i));
            IR2ToINT.setInputPin(i, IR2.getOut(i));
            IR3ToINT.setInputPin(i, IR3.getOut(i));
            IR23_8xToINT.setInputPin(i, IR1.getOut(7));
        }

        for (int i = 0; i < 8; i++) {
            IRDAint.setInputPin(i, IR3.getOut(i));
            IRDAint.setInputPin(8 + i, IR2.getOut(i));
        }

        for (int i = 0; i < 8; i++) {
            IRJAint.setInputPin(i, IR2.getOut(i));
            IRJAint.setInputPin(8 + i, IR1.getOut(i));
        }

        for (int i = 0; i < 8; i++) {
            IRPOMint.setInputPin(i, IR1.getOut(i));
            IRPOMint.setInputPin(8 + i, IR1.getOut(7));
        }

        for (int i = 0; i < 8; i++) {
            IRBRint.setInputPin(i, IR0.getOut(i));
        }

        TSBIRDAout3.setInputPin(0, IRDAint.getOut(0));
        TSBIRDAout3.setCtrl(Oper1.IRDAout3());
        Bus1.addOnIBUS3(TSBIRDAout3.getOut(0));

        TSBIRJAout2.setInputPin(0, IRJAint.getOut(0));
        TSBIRJAout2.setCtrl(Oper1.IRJAout2());
        Bus1.addOnIBUS2(TSBIRJAout2.getOut(0));

        TSBIRPOMout3.setInputPin(0, IRPOMint.getOut(0));
        TSBIRPOMout3.setCtrl(Oper1.IRPOMout3());
        Bus1.addOnIBUS3(TSBIRPOMout3.getOut(0));

        TSBIRBRout3.setInputPin(0, IRBRint.getOut(0));
        TSBIRBRout3.setCtrl(Oper1.IRBRout3());
        Bus1.addOnIBUS3(TSBIRBRout3.getOut(0));

    }

    public static void gui() {
        GuiLine line; // Pomocna promenljiva
        gui = new GuiScheme("tmhm/tmhm/images/Fetch1.png");

        ArrayList<ArrayList<Point>> sections;
        ArrayList<Point> points;



sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(592,7));
points.add(new Point(592,775));
sections.add(points);
line=new GuiLine(sections, Bus1.IBUS1());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(573,21));
points.add(new Point(573,760));
sections.add(points);
line=new GuiLine(sections, Bus1.IBUS2());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(554,37));
points.add(new Point(554,748));
sections.add(points);
line=new GuiLine(sections, Bus1.IBUS3());
gui.addLine(line);


sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(573,70));
points.add(new Point(370,70));
points.add(new Point(370,93));
sections.add(points);
line=new GuiLine(sections,Bus1.IBUS2());
gui.addLine(line);



sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(287,102));
points.add(new Point(294,102));
sections.add(points);
line=new GuiLine(sections, Oper1.incPC());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(287,121));
points.add(new Point(294,121));
sections.add(points);
line=new GuiLine(sections, Oper1.ldPC());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(445,112));
points.add(new Point(452,112));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);




sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(370,131));
points.add(new Point(370,251));
points.add(new Point(456,251));
sections.add(points);
points=new ArrayList<Point>();
points.add(new Point(370,204));
points.add(new Point(456,204));
sections.add(points);
points=new ArrayList<Point>();
points.add(new Point(370,157));
points.add(new Point(456,157));
sections.add(points);
line=new GuiLine(sections,PC.getOut(0));
gui.addLine(line);




sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,162));
points.add(new Point(467,168));
sections.add(points);
line=new GuiLine(sections,Oper1.PCout1());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,209));
points.add(new Point(467,215));
sections.add(points);
line=new GuiLine(sections,Oper1.PCHout3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,256));
points.add(new Point(467,262));
sections.add(points);
line=new GuiLine(sections,Oper1.PCLout3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(478,157));
points.add(new Point(592,157));
sections.add(points);
line=new GuiLine(sections,TSBPCout1.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(477,204));
points.add(new Point(554,204));
sections.add(points);
line=new GuiLine(sections, TSBPCHout3.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(477,251));
points.add(new Point(554,251));
sections.add(points);
line=new GuiLine(sections, TSBPCLout3.getOut(0));
gui.addLine(line);




sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(592,298));
points.add(new Point(226,298));
points.add(new Point(226,320));
sections.add(points);
points=new ArrayList<Point>();
points.add(new Point(441,298));
points.add(new Point(441,321));
sections.add(points);
line=new GuiLine(sections,IBUS1lowINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(144,331));
points.add(new Point(151,331));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(144,349));
points.add(new Point(151,349));
sections.add(points);
line=new GuiLine(sections,Oper1.ldIR0());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(226,359));
points.add(new Point(226,377));
sections.add(points);
line=new GuiLine(sections,IR0ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(359,331));
points.add(new Point(366,331));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(359,349));
points.add(new Point(366,349));
sections.add(points);
line=new GuiLine(sections,Oper1.ldIR1());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(441,359));
points.add(new Point(441,377));
sections.add(points);
line=new GuiLine(sections,IR1ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(592,404));
points.add(new Point(227,404));
points.add(new Point(227,427));
sections.add(points);
points=new ArrayList<Point>();
points.add(new Point(441,404));
points.add(new Point(441,427));
sections.add(points);
line=new GuiLine(sections,IBUS1lowINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(144,436));
points.add(new Point(151,436));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(144,455));
points.add(new Point(151,455));
sections.add(points);
line=new GuiLine(sections,Oper1.ldIR2());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(227,464));
points.add(new Point(227,481));
sections.add(points);
line=new GuiLine(sections,IR2ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(354,436));
points.add(new Point(366,436));
sections.add(points);
line=new GuiLine(sections,LogicalComponent.getCPUCLK());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,455));
points.add(new Point(366,455));
sections.add(points);
line=new GuiLine(sections,Oper1.ldIR3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(441,464));
points.add(new Point(441,481));
sections.add(points);
line=new GuiLine(sections,IR3ToINT.getOut(0));
gui.addLine(line);




sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,510));
points.add(new Point(380,510));
sections.add(points);
line=new GuiLine(sections,IR2ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,525));
points.add(new Point(380,525));
sections.add(points);
line=new GuiLine(sections,IR3ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(418,517));
points.add(new Point(456,517));
sections.add(points);
line=new GuiLine(sections,IRDAint.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,522));
points.add(new Point(467,528));
sections.add(points);
line=new GuiLine(sections,Oper1.IRDAout3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(478,517));
points.add(new Point(554,517));
sections.add(points);
line=new GuiLine(sections,TSBIRDAout3.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,566));
points.add(new Point(380,566));
sections.add(points);
line=new GuiLine(sections,IR1ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,581));
points.add(new Point(380,581));
sections.add(points);
line=new GuiLine(sections,IR2ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(418,574));
points.add(new Point(456,574));
sections.add(points);
line=new GuiLine(sections,IRJAint.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,579));
points.add(new Point(467,585));
sections.add(points);
line=new GuiLine(sections,Oper1.IRJAout2());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(478,574));
points.add(new Point(573,574));
sections.add(points);
line=new GuiLine(sections,TSBIRJAout2.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,623));
points.add(new Point(380,623));
sections.add(points);
line=new GuiLine(sections,IR23_8xToINT.getOut(0));
gui.addLine(line);
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(355,638));
points.add(new Point(380,638));
sections.add(points);
line=new GuiLine(sections,IR1ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(418,630));
points.add(new Point(456,630));
sections.add(points);
line=new GuiLine(sections,IRPOMint.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,635));
points.add(new Point(467,641));
sections.add(points);
line=new GuiLine(sections,Oper1.IRPOMout3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(478,630));
points.add(new Point(554,630));
sections.add(points);
line=new GuiLine(sections,TSBIRPOMout3.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(419,687));
points.add(new Point(456,687));
sections.add(points);
line=new GuiLine(sections,IR0ToINT.getOut(0));
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(467,692));
points.add(new Point(467,698));
sections.add(points);
line=new GuiLine(sections,Oper1.IRBRout3());
gui.addLine(line);
sections=new ArrayList<ArrayList<Point>>();
points=new ArrayList<Point>();
points.add(new Point(478,687));
points.add(new Point(554,687));
sections.add(points);
line=new GuiLine(sections,TSBIRBRout3.getOut(0));
gui.addLine(line);


        // LABELE:
        gui.addLabel(new GuiLabel(595, 33, Bus1.IBUS1()));
        gui.addLabel(new GuiLabel(577, 66, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(524, 100, Bus1.IBUS3()));
        gui.addLabel(new GuiLabel(400, 67, Bus1.IBUS2()));
        gui.addLabel(new GuiLabel(330, 162, PC.getOut(0)));
        gui.addLabel(new GuiLabel(507, 175, TSBPCout1.getOut(0)));
        gui.addLabel(new GuiLabel(512, 220, TSBPCHout3.getOut(0)));
        gui.addLabel(new GuiLabel(512, 265, TSBPCLout3.getOut(0)));
        gui.addLabel(new GuiLabel(340, 295, IBUS1lowINT.getOut(0)));
        gui.addLabel(new GuiLabel(340, 401, IBUS1lowINT.getOut(0)));
        gui.addLabel(new GuiLabel(220, 392, IR0ToINT.getOut(0)));
        gui.addLabel(new GuiLabel(435, 392, IR1ToINT.getOut(0)));
        gui.addLabel(new GuiLabel(220, 496, IR2ToINT.getOut(0)));
        gui.addLabel(new GuiLabel(435, 496, IR3ToINT.getOut(0)));;
        gui.addLabel(new GuiLabel(508, 512, TSBIRDAout3.getOut(0)));
        gui.addLabel(new GuiLabel(508, 569, TSBIRJAout2.getOut(0)));
        gui.addLabel(new GuiLabel(508, 626, TSBIRPOMout3.getOut(0)));
        gui.addLabel(new GuiLabel(508, 703, TSBIRBRout3.getOut(0)));

    }

    public static Pin IR31() {
        return IR0.getOut(7);
    }

    public static Pin IR30() {
        return IR0.getOut(6);
    }

    public static Pin IR29() {
        return IR0.getOut(5);
    }

    public static Pin IR28() {
        return IR0.getOut(4);
    }

    public static Pin IR27() {
        return IR0.getOut(3);
    }

    public static Pin IR26() {
        return IR0.getOut(2);
    }

    public static Pin IR25() {
        return IR0.getOut(1);
    }

    public static Pin IR24() {
        return IR0.getOut(0);
    }

    public static Pin IR23() {
        return IR1.getOut(7);
    }

    public static Pin IR22() {
        return IR1.getOut(6);
    }

    public static Pin IR21() {
        return IR1.getOut(5);
    }

    public static Pin PC() {
        return PC.getOut(0);
    }

    public static REG RegPC() {
        return PC;
    }

    public static REG RegIR0() {
        return IR0;
    }

    public static REG RegIR1() {
        return IR1;
    }

    public static REG RegIR2() {
        return IR2;
    }

    public static REG RegIR3() {
        return IR3;
    }
}
